package Sort;

public class InsertSort {
    /**
     * 直接插入排序
     */

    public static void insertSort(int[] arr){
        for (int i = 1; i <arr.length; i++) {
            int tmp=arr[i];
            int j=i-1;
            while (j>=0){
                if(arr[j]>tmp){
                    arr[j+1]=arr[j];
                    j--;
                }else {
                    arr[j+1]=tmp;
                    break;
                }
            }
            arr[j+1]=tmp;
        }
    }
    public static void main(String[] args) {
        int[] array={9,8,7,6,5,4,3,2,1};
        insertSort(array);
        for (int i = 0; i <array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }
}
